package com.yinp.proapp.module.study.recycler.activity;

import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.yinp.proapp.R;
import com.yinp.proapp.adapter.CommonAdapter;
import com.yinp.proapp.adapter.SingleViewHolder;
import com.yinp.proapp.base.activity.AppBaseActivity;
import com.yinp.proapp.databinding.ActivityMoreListBinding;
import com.yinp.proapp.databinding.ItemMoreListOneBinding;
import com.yinp.proapp.databinding.ItemMoreListTwoBinding;
import com.yinp.proapp.module.study.recycler.bean.OneMoreListBean;
import com.yinp.proapp.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yinp
 * @Date: 2021/8/30
 * @Description:Recycler嵌套使用
 */
public class MoreListActivity extends AppBaseActivity<ActivityMoreListBinding> {
    private CommonAdapter<OneMoreListBean> oneAdapter;
    private List<OneMoreListBean> oneList = new ArrayList<>();

    private List<String> twoList = new ArrayList<>();
    private CommonAdapter<String> twoAdapter;

    private List<String> threeList = new ArrayList<>();
    private CommonAdapter<String> threeAdapter;

    private SmartRefreshLayout baseRefresh;

    @Override
    protected void initViews() {
        setStatusBarHeight(StatusBarUtil.getStatusBarHeight(mContext));
        initClick(bd.header.headerBackImg);
        bd.header.headerCenterTitle.setText("RecyclerView嵌套处理");
        initData();
        initRecycler();
        fresh();
    }

    private void fresh() {
        baseRefresh = findViewById(R.id.base_refresh);
        baseRefresh.setOnScrollChangeListener((v, scrollX, scrollY, oldScrollX, oldScrollY) -> {

        });
        baseRefresh.setEnableNestedScroll(false);
        baseRefresh.setOnRefreshListener(refreshLayout -> {
            new Handler().postDelayed(() -> baseRefresh.finishRefresh(true), 1000);
        });
        baseRefresh.setOnLoadMoreListener(refreshLayout -> {
            new Handler().postDelayed(() -> baseRefresh.finishLoadMore(true), 1000);
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == bd.header.headerBackImg) {
            finish();
        }
    }

    private void initData() {
        for (int i = 0; i < 14; i++) {
            oneList.add(new OneMoreListBean(String.valueOf(i)));
            if (i <= 4) {
                twoList.add(String.valueOf(i));
                twoList.add(String.valueOf(i));
                twoList.add(String.valueOf(i));
                twoList.add(String.valueOf(i));
                twoList.add(String.valueOf(i));
            }
            threeList.add(String.valueOf(i));
        }
    }

    private void initRecycler() {
        oneAdapter = new CommonAdapter<OneMoreListBean>(mContext, oneList) {
            @Override
            protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent) {
                ItemMoreListOneBinding binding = ItemMoreListOneBinding.inflate(mInflater, parent, false);
                SingleViewHolder viewHolder = new SingleViewHolder(binding);

                return viewHolder;
            }

            @Override
            public void onBindItem(SingleViewHolder holder, int position, OneMoreListBean item) {
                ItemMoreListOneBinding binding = (ItemMoreListOneBinding) holder.getBinding();
                binding.tvTitle.setText("第" + item.getTitle() + "个");
                binding.tvTitle.setOnClickListener(v -> Log.d("abcd", "onClick:aaa "));
                if (item.getTwoAdapter() == null) {
                    item.setTwoAdapter(new CommonAdapter<String>(mContext, twoList) {
                        @Override
                        protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent2) {
                            return new SingleViewHolder(ItemMoreListTwoBinding.inflate(mInflater, parent2, false));
                        }

                        @Override
                        public void onBindItem(SingleViewHolder holder, int position2, String item2) {
                            ItemMoreListTwoBinding binding = (ItemMoreListTwoBinding) holder.getBinding();
                            binding.tvTitle.setText("第" + item2 + "个");
                            holder.itemView.setOnClickListener(MoreListActivity.super::onClick);
                        }

                        @Override
                        public long getItemId(int position) {
                            return position;
                        }
                    });
                    binding.rvTwo.setLayoutManager(new LinearLayoutManager(mContext));
                    item.getTwoAdapter().setHasStableIds(true);
                    binding.rvTwo.setAdapter(item.getTwoAdapter());
                }
            }

            @Override
            public long getItemId(int position) {
                return position;
            }
        };
        LinearLayoutManager llm = new LinearLayoutManager(this);
        bd.rvOne.setLayoutManager(llm);
        oneAdapter.setHasStableIds(true);
        bd.rvOne.setAdapter(oneAdapter);
    }
}
